﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain;
using Domain.Dto;
using Domain.Interface;
using Domain.Model;
using Gmmy.DomainExtension;
using Surging.Core.CPlatform.Ioc;
using Gmmy.RepositoryExtension;
using Gmmy.Utility;
using Gmmy.Utility.Extensions;
using Infrastructure.Repository.DataContext;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.Repository.Repository.Sql
{
    public class UserRepoistory : RepositoryBase<User>, IUserRepoistory
    {
       
        public UserRepoistory(IDataBaseFactory<EfContext> dataBaseFactory
            ) : base(dataBaseFactory)
        {

        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="input"></param>
        /// <param name="specification"></param>
        /// <returns></returns>
        public async Task<EntityList<UserListDto>> GetList(UserListInput input,
            ISpecification<UserListInput> specification)
        {
            var result = new EntityList<UserListDto>();
            var query = (from a in dbset
                select new UserListInput()
                {
                    ID = a.ID,
                    LoginName = a.LoginName,
                    NickName = a.NickName,
                    State = a.State,
                    CreateTime = a.CreateTime,
                    LastLoginIp = a.LastLoginIp,
                    LastLoginTime = a.LastLoginTime,
                    IsDeleted = a.IsDeleted,

                }).Where(specification.SatisfiedBy());
            if (input.OrderParm == null || !input.OrderParm.Any())
            {
                input.OrderParm = new Dictionary<string, string> { { "CreateTime", "DESC" } };
            }
            query = DataSort(query, input.OrderParm);
            var data = await FindAllAsyncAsQuery(query, input.PageIndex, input.PageSize);
            result = data.MapTo<EntityList<UserListDto>, EntityList<UserListInput>>();
            result.Data.ForEach(x=>x.StateName=x.State.GetLocalizedDescription());
            return result;
        }
    }
}
